/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sgs.dao;

import br.com.sgs.entity.Aluno;
import java.util.Date;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Eric
 */
public class DAOAluno extends AbstractDAO implements IDAOAluno{
    
    public DAOAluno(){
        super();
    }
    
     @Override
    public Aluno findAlunoById(int idaluno) {
        try {

            Query q = em.createQuery("SELECT a FROM Aluno a WHERE a.id = :idaluno");
            q.setParameter("idaluno", idaluno);
            return (Aluno) q.getSingleResult();

        } catch (Exception e) {
            System.err.println("Erro no findCampusById");
            return null;
        }
    }
   
    @Override
    public List<Aluno> findAlunoByAll(String nome, String email, String rgm, String rg, String cpf, Date dtcriacaoini, Date dtcriacaofin, List<String> lsstatus) {

        try {

            StringBuilder sql = new StringBuilder();

            if (!nome.equals("")) {
                sql.append("  AND UPPER(a.nome) LIKE '%").append(nome.toUpperCase()).append("%'");
            }

            if (!email.equals("")) {
                sql.append("  AND UPPER(a.email) LIKE '%").append(email.toUpperCase()).append("%'");
            }

            if (!rgm.equals("")) {
                sql.append("  AND UPPER(a.rgm) LIKE '%").append(rgm.toUpperCase()).append("%'");
            }

            if (!rg.equals("")) {
                sql.append("  AND UPPER(a.rg) LIKE '%").append(rg.toUpperCase()).append("%'");
            }

            if (!cpf.equals("")) {
                sql.append("  AND UPPER(a.cpf) LIKE '%").append(cpf.toUpperCase()).append("%'");
            }

            if (dtcriacaoini != null && dtcriacaofin != null) {
                sql.append(" AND a.dtcriacao BETWEEN :dtcriacaoini AND :dtcriacaofin ");
            }

            if (!lsstatus.isEmpty()) {
                String status = "";
                for (String st : lsstatus) {
                    status += st + ",";
                }
                status += lsstatus.get(0).toString();
                sql.append(" AND a.status  IN (").append(status).append(")");
            }

            Query q = em.createQuery("SELECT a FROM Aluno a WHERE a.id > 0 " + sql.toString() + " ORDER BY a.nome ASC");

            if (dtcriacaoini != null && dtcriacaofin != null) {
                q.setParameter("dtcriacaoini", dtcriacaoini);
                q.setParameter("dtcriacaofin", dtcriacaofin);
            }

            return q.getResultList();

        } catch (Exception e) {
            System.err.println("Erro na consulta findInstituicaByAll");
            e.printStackTrace();
            return null;
        }
    }

}
